package com.sky.directdeal.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.internal.view.SupportMenuInflater;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sky.directdeal.R;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.crouton.Style;
import com.sky.directdeal.manager.ImageLoader;
import com.sky.directdeal.utils.Utils;
import com.sky.local_ads.store_list.StoreDetails;

public class BusinessDetailsActivity extends BaseActivity implements OnClickListener {
	private TextView nameText;
	private TextView addressText;
	private TextView phoneText;
	private TextView emailText;
	private TextView websiteText;
	private TextView descText;
	private ImageView businessImageView;
	private Button viewOfferButton;
	private StoreDetails storeDetails;
	
	private ImageLoader imageLoader;
	
	private ShareActionProvider mShareActionProvider;
	
	private String shareText=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_business_details);
		imageLoader = new ImageLoader(getApplicationContext(), R.drawable.loading_2x2, R.drawable.noimage_2x2);
		setHomeUp();
		initializeUI();
		getBundleData(this);
	}

	
	private void getBundleData(Context context){
		if(getIntent().getExtras()!=null && getIntent().hasExtra(Constants.BUSINESS_DETAILS_INTENT)){
			storeDetails=(StoreDetails) getIntent().getSerializableExtra(Constants.BUSINESS_DETAILS_INTENT);
			if(storeDetails!=null){
				setHeaderTitle(storeDetails.getBusinessName()+" "+getString(R.string.details));
				setTextValues(storeDetails);
			}
			
		}
	}
	
	private void initializeUI(){
		nameText=(TextView)findViewById(R.id.textBusinessName);
		addressText=(TextView)findViewById(R.id.textBusinessAddress);
		phoneText=(TextView)findViewById(R.id.textBusinessPhone);
		emailText=(TextView)findViewById(R.id.textBusinessEmail);
		websiteText=(TextView)findViewById(R.id.textBusinessWebsite);
		descText=(TextView)findViewById(R.id.textBusinessDesc);
		businessImageView=(ImageView)findViewById(R.id.imageViewBusinessImage);
		viewOfferButton=(Button)findViewById(R.id.buttonViewOffers);
		viewOfferButton.setOnClickListener(this);
		
	}
	
	private void setTextValues(StoreDetails details){
		nameText.setText(details.getBusinessName());
		addressText.setText(details.getAddress());
		
		final String phNo=details.getPhoneNumber();
		RelativeLayout storeCallInnerLayout = (RelativeLayout)findViewById(R.id.storeCallLayout);
		if(phNo==null){
			storeCallInnerLayout.setVisibility(View.GONE);
		}
		else{
			storeCallInnerLayout.setVisibility(View.VISIBLE);
			phoneText.setText(phNo);
		}
		
		storeCallInnerLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Utils.callNumber(phNo, BusinessDetailsActivity.this);
			}
		});
		
		emailText.setText(details.getEmail());
		websiteText.setText(details.getWebsite());
		descText.setText(details.getDesc());
		imageLoader.displayImage("http://www.filmyfriday.com/wp-content/uploads/2014/01/86748-thumb.jpg", businessImageView);
		if(details.getBusinessName()!=null){
			shareText=details.getBusinessName()+"\n";
		}
		if(details.getPhoneNumber()!=null){
			shareText=shareText+details.getPhoneNumber()+"\n";
		}
		if(details.getAddress()!=null){
			shareText=shareText+details.getAddress()+"\n";
		}
		if(details.getEmail()!=null){
			shareText=shareText+details.getEmail()+"\n";
		}
		if(details.getWebsite()!=null){
			shareText=shareText+details.getWebsite();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonViewOffers:
			Intent storeOfferIntent=new Intent(this,StoreOffersListActivity.class);
			storeOfferIntent.putExtra(Constants.BUSINESS_DETAILS_INTENT, storeDetails);
			startActivity(storeOfferIntent);
			break;

		default:
			break;
		}
		
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		case R.id.action_share:
			if(shareText!=null){
				Utils.ShareIntentMessage(BusinessDetailsActivity.this, shareText);
			}
			else{
				Utils.displayToast(BusinessDetailsActivity.this, getString(R.string.sorry_share_not_available),Style.INFO);
			}
			
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		new SupportMenuInflater(getApplication()).inflate(R.menu.business_details, menu); 
		 // Locate MenuItem with ShareActionProvider
	  /* MenuItem item = menu.findItem(R.id.action_share);

	    // Fetch and store ShareActionProvider
	   mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
	   if(mShareActionProvider!=null)
	   mShareActionProvider.setShareIntent(getDefaultIntent());*/
		return super.onCreateOptionsMenu(menu);
		
	}
	
	private Intent getDefaultIntent() {
	    Intent intent = new Intent(Intent.ACTION_SEND);
	    intent.setType("image/*");
	    return intent;
	}
	
}
